<?php

/**
 * This is the model class for table "{{society}}".
 *
 * The followings are the available columns in table '{{society}}':
 * @property string $idsociety
 * @property string $societyname
 * @property string $idblock
 * @property string $societyaddress
 * @property string $societycontact
 * @property string $societyemail
 * @property string $societyassistant
 * @property string $assistantcontact
 * @property string $assistantemail
 * @property string $assistantaddress
 * @property string $remark
 *
 * The followings are the available model relations:
 * @property Demand[] $demands
 * @property Distribution[] $distributions
 * @property Block $idblock0
 * @property Stocktransfer[] $stocktransfers
 */
class Society extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Society the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{society}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('societyname, idblock, societyassistant', 'required'),
			array('societyname, societycontact, societyemail, societyassistant, assistantcontact, assistantemail', 'length', 'max'=>45),
			array('idblock', 'length', 'max'=>10),
			array('societyaddress, assistantaddress, remark', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idsociety, societyname, idblock, societyaddress, societycontact, societyemail, societyassistant, assistantcontact, assistantemail, assistantaddress, remark', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'demands' => array(self::HAS_MANY, 'Demand', 'idsociety'),
			'distributions' => array(self::HAS_MANY, 'Distribution', 'idsociety'),
			'idblock0' => array(self::BELONGS_TO, 'Block', 'idblock'),
			'stocktransfers' => array(self::HAS_MANY, 'Stocktransfer', 'idsociety'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idsociety' => 'Idsociety',
			'societyname' => 'Societyname',
			'idblock' => 'Idblock',
			'societyaddress' => 'Societyaddress',
			'societycontact' => 'Societycontact',
			'societyemail' => 'Societyemail',
			'societyassistant' => 'Societyassistant',
			'assistantcontact' => 'Assistantcontact',
			'assistantemail' => 'Assistantemail',
			'assistantaddress' => 'Assistantaddress',
			'remark' => 'Remark',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
                if(isset($_REQUEST['idblock'])){
                    Yii::app()->session['idblock'] = $_REQUEST['idblock'];
                    $this->idblock = $_REQUEST['idblock'];
//                    $criteria->compare('idblock',$_REQUEST['idblock'],true);                    
                }
                else{
                    if(isset(Yii::app()->session['idblock'])){
                        $this->idblock = Yii::app()->session['idblock'];
//                        $criteria->compare('idblock',Yii::app()->session['idblock'],true);
                    }                    
                }
                
//                $this->idblock = 0;
//                echo $this->idblock;

		$criteria->compare('idsociety',$this->idsociety,true);
		$criteria->compare('societyname',$this->societyname,true);
		$criteria->compare('idblock',$this->idblock);
//		$criteria->compare('idblock',0,true);
		$criteria->compare('societyaddress',$this->societyaddress,true);
		$criteria->compare('societycontact',$this->societycontact,true);
		$criteria->compare('societyemail',$this->societyemail,true);
		$criteria->compare('societyassistant',$this->societyassistant,true);
		$criteria->compare('assistantcontact',$this->assistantcontact,true);
		$criteria->compare('assistantemail',$this->assistantemail,true);
		$criteria->compare('assistantaddress',$this->assistantaddress,true);
		$criteria->compare('remark',$this->remark,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}